package com.fyzk.activity.user;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.RadioGroup;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.lottery.togetherbuy.HM_Popupwindow;
import com.fyzk.activity.lottery.togetherbuy.HM_VerticalPop;
import com.fyzk.bean.TogetherBuyRecordItem;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

import http.Common.CommandUtil;
import http.StringResultBean;

@ContentView(R.layout.ac_usercenter_togetherbuy)
public class UserCenterTogetherBuyActivity extends RecordBaseActivity implements
        onTopTitleClickListener, HM_VerticalPop.HM_PopuponItemListener {

    @ViewInject(R.id.user_zhuihao_topTitle)
    private TopTitleView topView;
    @ViewInject(R.id.user_zhuihao_content)
    private UserCenterHeMaiRecordView togetherbuyRecordView;

    @ViewInject(R.id.prizestatus_filter)
    private RadioGroup prizestatus_filter;

    private final int ALL = 0;
    private final int PRIZEED = 1;
    private final int UNOPEN = 2;

    private int winState = ALL;
    private int pageIndex = 1;

    private String[] popup_items = { "全部", "近一周","近一个月", "近两个月"};
    private int[] range_time = {1,2,3,4};
    private HM_VerticalPop top_popup;
    protected int timeRange = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        net();
    }

    @Override
    public void update() {
        isUpdate = true;
        // TODO Auto-generated method stub
        pageIndex = 1;
        net();
    }

    @Override
    protected void init() {
        // TODO Auto-generated method stub
        super.init();
        topView.setCenterText(getString(R.string.usercenter_hemai));
        togetherbuyRecordView.setMoreInterface(this);
        togetherbuyRecordView.setUpdateInterface(this);
        togetherbuyRecordView.setRetryInterface(this);
        topView.setRightTxt("近一周");
        topView.setOnTitleClickListener(this);
        topView.getRightTxt().setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.arrow_under,null),null);

        top_popup = new HM_VerticalPop(this, AbsListView.LayoutParams.WRAP_CONTENT, AbsListView
                .LayoutParams.WRAP_CONTENT,
                popup_items);
        top_popup.setHm_PopupOnItemListener(this);

        prizestatus_filter.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.prizestatus_all:
                        winState = ALL;
                        break;
                    case R.id.prizestatus_prized:
                        winState = PRIZEED;
                        break;
                    case R.id.prizestatus_unopen:
                        winState = UNOPEN;
                        break;
                }
                pageIndex = 1;
                togetherbuyRecordView.netProgressView.waiting();
                net();
            }
        });

    }


    @Override
    protected void onFailure(int requestCode, String errorCode, String message) {
        if (errorCode.equals("900003")) {
            if (pageIndex == 1) {
                togetherbuyRecordView.netProgressView.setVisibility(View.VISIBLE);
                togetherbuyRecordView.netProgressView.noneRecord(message);
            } else {
                togetherbuyRecordView.listViewExt.listMore.setClickable(false);
                togetherbuyRecordView.listViewExt.listMore.setTextStr("无更多数据");
            }
        } else {
            togetherbuyRecordView.netProgressView.fail(message);
        }

        togetherbuyRecordView.listAdapter.notifyDataSetChanged();
    }

    private void requestChaseList() {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("winState", winState);//合买状态 0全部 1中奖 2待开奖
            request_json.put("pageIndex", pageIndex);
            request_json.put("maxResult", 20);
            request_json.put("timeRange", timeRange);  // 查询时间段 1全部 2一周 3一个月 4两个月
            httpPostJson(CommandUtil.FINDHISTORYCASELOTBET, Commands.FINDHISTORYCASELOTBET,
                    request_json,
                    false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public void net() {
        if (togetherbuyRecordView.infos == null || togetherbuyRecordView.infos.size() == 0) {
            togetherbuyRecordView.showLoading();
        }
        requestChaseList();
    }

    @Override
    public void reTry() {
        // TODO Auto-generated method stub
        net();
    }


    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        if (resultBean.getErrorcode().equals("0")) {
            try {
                JSONObject object = new JSONObject(resultBean.getData());
                String string = object.getString("caselots");
                ArrayList togetherBuyRecordItems = (ArrayList) JSON.parseArray(string,
                        TogetherBuyRecordItem.class);
                if (pageIndex == 1) {
                    if(togetherBuyRecordItems.size() == 0){
                        togetherbuyRecordView.netProgressView.noneRecord(resultBean.getMessage());
                    }else{
                        togetherbuyRecordView.initRecordList(togetherBuyRecordItems);
                    }
                } else {
                    togetherbuyRecordView.addRecordInfos(togetherBuyRecordItems);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            if (isUpdate) {
                togetherbuyRecordView.listViewExt.onRefreshComplete();
            }

        }
    }

    @Override
    public void more() {
        isUpdate = false;
        pageIndex++;
        net();
    }

    @Override
    public void onRightClick() {
        top_popup.showPopupWindow(topView.getRightTxt());
    }

    @Override
    public void onItem(int position) {
        topView.setRightTxt(popup_items[position]);
        timeRange = range_time[position];
        pageIndex = 1; // pages重置为第一页
        net();
    }

    @Override
    public void popupShow() {

    }

    @Override
    public void popupDismiss() {

    }
}
